package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Classes;
import com.ruoyi.system.domain.Student;
import com.ruoyi.system.domain.vo.StudentVo;
import com.ruoyi.system.mapper.ClassesMapper;
import com.ruoyi.system.mapper.StudentMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.GradesMapper;
import com.ruoyi.system.domain.Grades;
import com.ruoyi.system.service.IGradesService;

import javax.annotation.Resource;

/**
 * 年级Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-15
 */
@Service
public class GradesServiceImpl implements IGradesService {

    @Autowired
    private GradesMapper gradesMapper;

    @Resource
    private ISysDeptService sysDeptService;

    @Resource
    private ClassesMapper classesMapper;

    @Resource
    private StudentMapper studentMapper;

    /**
     * 查询年级
     *
     * @param gid 年级主键
     * @return 年级
     */
    @Override
    public Grades selectGradesByGid(Long gid) {
        return gradesMapper.selectGradesByGid(gid);
    }

    /**
     * 根据登录学校查到所有年级
     *
     * @return
     */
    @Override
    public List<Grades> selectGradesToSid() {
        Integer sidToLoginUser = sysDeptService.getSidToLoginUser();
        return gradesMapper.selectGradesToSid(sidToLoginUser);
    }

    /**
     * 用户分班,导出整个年级的学生信息
     *
     * @return
     */
    @Override
    public List<StudentVo> exportAllStudent(Integer gid) {
        List<StudentVo> studentList = new ArrayList<>();
        List<Classes> classes = classesMapper.selectClassByGid(gid);
        for (int j = 0; j < classes.size(); j++) {
            List<Student> studentList1 = studentMapper.selectOneStudentMin(gid, classes.get(j).getCid());
            for (int k = 0; k < studentList1.size(); k++) {
                StudentVo studentVo = new StudentVo();
                studentVo.setStudentName(studentList1.get(k).getStudentName());
                studentVo.setStudentSex(studentList1.get(k).getStudentSex());
                studentVo.setCardNumber(studentList1.get(k).getCardNumber());
                studentVo.setStudentBelongToClass(studentList1.get(k).getStudentBelongToClass());
                studentList.add(studentVo);
            }
        }
        return studentList;
    }

    /**
     * 查询年级列表
     *
     * @param grades 年级
     * @return 年级
     */
    @Override
    public List<Grades> selectGradesList(Grades grades) {
        return gradesMapper.selectGradesList(grades);
    }

    /**
     * 新增年级
     *
     * @param grades 年级
     * @return 结果
     */
    @Override
    public int insertGrades(Grades grades) {
        Integer sidToLoginUser = sysDeptService.getSidToLoginUser();
        grades.setSid(Long.valueOf(sidToLoginUser));
        grades.setCreateTime(DateUtils.getNowDate());
        return gradesMapper.insertGrades(grades);
    }

    /**
     * 修改年级
     *
     * @param grades 年级
     * @return 结果
     */
    @Override
    public int updateGrades(Grades grades) {
        return gradesMapper.updateGrades(grades);
    }

    /**
     * 批量删除年级
     *
     * @param gids 需要删除的年级主键
     * @return 结果
     */
    @Override
    public int deleteGradesByGids(Long[] gids) {
        return gradesMapper.deleteGradesByGids(gids);
    }

    /**
     * 删除年级信息
     *
     * @param gid 年级主键
     * @return 结果
     */
    @Override
    public int deleteGradesByGid(Long gid) {
        return gradesMapper.deleteGradesByGid(gid);
    }
}
